import htmlParser from '@/utils/html-parser.js'
export function transName(val,option) {
  const arr = option || []
  let res = ''
  arr.forEach(item => {
    if (item.value == val) {
      res = item.label;
    }
  });
  return res
}

export function getValue(code,option) {
  const arr = option || []
  let res = ''
  arr.forEach(item => {
    if (item.code === code) {
      res = item.value;
    }
  });
  return res
}
// 转富文本
export function transHtml(val){
	let res = ''
	if(val){
		res = htmlParser(val) 
	}else{
		res = '暂无介绍'
	}
	return res
}
// 转星期
export function transWeek(val){
	let result = '';
	if(val==1){
		result = '周一';
	}else if(val==2) {
		result = '周二';
	}else if(val==0) {
		result = '周日';
	}else if(val==3) {
		result = '周三';
	}else if(val==4) {
		result = '周四';
	}else if(val==5) {
		result = '周五';
	}else if(val==6) {
		result = '周六';
	}
	return result;
}

// 获取URL上参数
export function getUrlParam(name){console.log(window.location)
	var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)')  
	// let url = window.location.href.split('#')[1]
	// let search = url.split('?')[1]
  let search = window.location.search.split('?')[1]
  console.log(111, search)
	if (search) {  
		var r = search.substr(0).match(reg)  
		if (r !== null) return unescape(r[2])
		return null  
	} else {  
		return null  
	}  
}

// 获取URL的路径
export function getUrlPath(){   
	let url = window.location.href||document.location.toString()
	let arrUrl = url.split("//");
	let start = arrUrl[1].indexOf("/");
	let relUrl = arrUrl[1].substring(start);
	if(relUrl.indexOf("?") != -1){
		relUrl = relUrl.split("?")[0];
	}
	return relUrl;
}

// 重新获取code
export function getAuth(appid){
	const link = window.location.href
	let uri = encodeURIComponent(link)
	let authURL = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${uri}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect`;  
	window.location.href = authURL
}

export function getWeekAndDay(date) {
  // 将日期转换为一年中的第几周
  var oneJan = new Date(date.getFullYear(), 0, 1);
  var numDays = (date - oneJan) / (24 * 60 * 60 * 1000);
  // var weekNumber = Math.ceil((date.getDay() + 6 - oneJan.getDay()) / 7);
  var weekNumber = Math.ceil(numDays/7);
  let str = '日一二三四五六'
  let d = date.getDay()
  let c = str.charAt(d)
  let m=date.getMonth()+1

  return {
    week:date.getFullYear()+ "年"+m+"月 第"+weekNumber+"周",
    zhou:"第"+weekNumber+"周",
    day: "星期"+c
  };
}

